//日期自定义格式化
function dateFormat(fmt, date) {
	let ret;
	let opt = {
		"Y+": date.getFullYear().toString(), // 年
		"m+": (date.getMonth() + 1).toString(), // 月
		"d+": date.getDate().toString(), // 日
		"H+": date.getHours().toString(), // 时
		"M+": date.getMinutes().toString(), // 分
		"S+": date.getSeconds().toString() // 秒
		// 有其他格式化字符需求可以继续添加，必须转化成字符串
	};
	for (let k in opt) {
		ret = new RegExp("(" + k + ")").exec(fmt);
		if (ret) {
			fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
		};
	};
	return fmt;
}

//获取当天日期
function getToday() {
	let date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate()
	let week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];
	let currentTime = year + "年" + month + "月" + day + "日 " + " " + week;
	return currentTime
}

//获取最近7天日期
function getWeek() {
	let weeks = []
	for (let i = 0; i < 7; i++) {
		let date = new Date()
		date.setTime(date.getTime() + i * 24 * 60 * 60 * 1000);
		let week = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六")[date.getDay()];
		weeks.push({day:date.getDate(),month:date.getMonth()+1,week:week})
	}
	return weeks
}

const label = 'huahejia_'

//储存用户
function setUser(user) {
	uni.setStorageSync(label+'user', JSON.stringify(user))
}

//移除用户
function removeUser() {
	uni.clearStorageSync()
	setTimeout(() => {
		uni.reLaunch({
			url: '/pages/login-register/login'
		})
	}, 1500)
}

//获取用户
function getUser(){
	let user = uni.getStorageSync(label+'user')
	if(user){
		let res = JSON.parse(user)
		if(res)
			return res
	}else{
		uni.$u.toast('暂无用户信息')
	}
}

//储存token
function setToken(token) {
	uni.setStorageSync(label+'token', token)
}

//获取token
function getToken() {
	let token = uni.getStorageSync(label+'token')
	if(token)
		return token
	else
		return false
}


//预览图片
function previewImage(urls,index) {	
	if(Array.isArray(urls)){
		// 如果是多图预览
		uni.previewImage({
			current: index, //预览图片的下标
			urls: urls ,//预览图片的地址，必须要数组形式，如果不是数组形式就转换成数组形式就可以
			loop:true,// 是否可循环预览
			indicator:'number' //指示器
		})
	}else{
		// 如果是单图预览
		let arr = []
		arr.push(urls)
		uni.previewImage({
			current:0,
			urls:arr
		})
	}
}

export {
	dateFormat,
	setUser,
	removeUser,
	getUser,
	getToken,
	setToken,
    previewImage
}
